using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using TST.SharePointObjects.SecurityBluePrint.Timerjobs;

namespace TST.SharePointObjects.SecurityBluePrint.FeatureReceivers
{
    public class SecurityBluePrintTimerJobFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

            foreach (SPJobDefinition job in webApplication.JobDefinitions)
            {
                if (job.Name == Globals.SECURITYBLUEPRINTJOBNAME)
                {
                    job.Delete();
                    break;
                }
            }

            CreateBluePrintsTimerJob blueprintJob = new CreateBluePrintsTimerJob(Globals.SECURITYBLUEPRINTJOBNAME, webApplication);

            foreach (SPFeatureProperty featureProperty in properties.Feature.Properties)
            {
                if ((!string.IsNullOrEmpty(featureProperty.Name)) && (!string.IsNullOrEmpty(featureProperty.Value)))
                {
                    if (blueprintJob.Properties.ContainsKey(featureProperty.Name))
                    {
                        blueprintJob.Properties[featureProperty.Name] = featureProperty.Value;
                    }
                    else
                    {
                        blueprintJob.Properties.Add(featureProperty.Name, featureProperty.Value);
                    }
                }
            }

            // Create the time
            int hour = 0;
            int minute = 0;
            string[] time = null;
            if (blueprintJob.Properties["TST.SharePointObjects.SecurityBluePrint.CreateBluePrintsTimerJob.Time"] != null)
                time = blueprintJob.Properties["TST.SharePointObjects.SecurityBluePrint.CreateBluePrintsTimerJob.Time"].ToString().Split(new char[] { ':' });
            if (time != null)
            {
                int.TryParse(time[0], out hour);
                int.TryParse(time[1], out minute);
            }

            // Determine the Time Schema
            String schema = "day";
            if (blueprintJob.Properties["TST.SharePointObjects.SecurityBluePrint.CreateBluePrintsTimerJob.Schedule"] != null)
            {
                schema = blueprintJob.Properties["TST.SharePointObjects.SecurityBluePrint.CreateBluePrintsTimerJob.Schedule"].ToString();
            }
            switch (schema.ToLower())
            {
                default:
                case "day":
                    SPDailySchedule scheduleDay = new SPDailySchedule();
                    scheduleDay.BeginHour = hour;
                    scheduleDay.BeginMinute = minute;
                    scheduleDay.BeginSecond = 0;
                    scheduleDay.EndHour = (scheduleDay.BeginHour + 1);
                    scheduleDay.EndMinute = 0;
                    scheduleDay.EndSecond = 0;
                    blueprintJob.Schedule = scheduleDay;
                    break;
                case "week":
                    SPWeeklySchedule scheduleWeek = new SPWeeklySchedule();
                    scheduleWeek.BeginDayOfWeek = DayOfWeek.Sunday;
                    scheduleWeek.BeginHour = hour;
                    scheduleWeek.BeginMinute = minute;
                    scheduleWeek.BeginSecond = 0;
                    scheduleWeek.EndDayOfWeek = DayOfWeek.Sunday;
                    scheduleWeek.EndHour = (scheduleWeek.BeginHour + 1);
                    scheduleWeek.EndMinute = 0;
                    scheduleWeek.EndSecond = 0;
                    blueprintJob.Schedule = scheduleWeek;
                    break;
                case "hour":
                    SPHourlySchedule scheduleHour = new SPHourlySchedule();
                    scheduleHour.BeginMinute = minute;
                    blueprintJob.Schedule = scheduleHour;
                    break;
            }
            blueprintJob.Update();
            blueprintJob.Provision();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

            foreach (SPJobDefinition job in webApplication.JobDefinitions)
            {
                if (job.Name == Globals.SECURITYBLUEPRINTJOBNAME)
                {
                    job.Delete();
                }
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}
